﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-30-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
namespace NMortgage.UI.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using FluentValidation;

    public sealed class FeeCollectionValidator : AbstractValidator<FeeCollection>
    {
        #region Constructors

        public FeeCollectionValidator()
        {
            RuleFor(p => p.Count).GreaterThan(0);
            RuleFor(p => p.Count).Must(HaveValidChildren);
        }

        #endregion Constructors

        #region Methods

        public bool HaveValidChildren(FeeCollection instance, int e)
        {
            return instance.Count(p =>
            {
                IValidatable vp = p as IValidatable;
                return ((vp != null && !vp.IsValid) ? true : false);
            }) == 0;
        }

        #endregion Methods
    }

    public sealed class FeeValidator : AbstractValidator<Fee>
    {
        #region Fields

        private const int MONTHS_IN_YEAR = 12;

        #endregion Fields

        #region Constructors

        public FeeValidator()
        {
            RuleFor(r => r.Amount)
                .GreaterThan(0);

            RuleFor(r => r.Months)
                .GreaterThanOrEqualTo(0)
                .LessThanOrEqualTo(MONTHS_IN_YEAR);

            RuleFor(r => r.Years)
                .GreaterThanOrEqualTo(0)
                .LessThanOrEqualTo(100);

            RuleFor(r => r.Months)
                .Must(HaveYearsMonthsGreaterThanZero)
                .WithMessage(Properties.Resources.ValidationError_YearsMonthsGreaterThanZero);
            RuleFor(r => r.Years)
                .Must(HaveYearsMonthsGreaterThanZero)
                .WithMessage(Properties.Resources.ValidationError_YearsMonthsGreaterThanZero);
        }

        #endregion Constructors

        #region Methods

        private bool HaveYearsMonthsGreaterThanZero(Fee instance, int i)
        {
            return instance.TotalDurationInMonths > 0;
        }

        #endregion Methods
    }
}